﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Xml.Serialization;
using Startup.Shell.Domain.FunctionalityContracts;

namespace Startup.Shell.Domain
{
	[XmlInclude(typeof(LoadCommandContract))]
	[XmlInclude(typeof(SaveCommandContract))]
	[XmlInclude(typeof(NewCommandContract))]
	public class StartupClass
	{
		public string Name { get; set; }
		public ObservableCollection<StartupProperty> Properties { get; set; }
		public ObservableCollection<CommandContractBase> FunctionalityContracts { get; set; }

		public StartupClass()
		{
			Properties = new ObservableCollection<StartupProperty>();
			FunctionalityContracts = new ObservableCollection<CommandContractBase>();
		}

		public override string ToString()
		{
			return Name;
		}
		
		public IList<StartupProperty> AllProperties
		{
			get
			{
				List<StartupProperty> result = new List<StartupProperty>();

				for (int index = 0; index < Properties.Count; index++)
				{
					StartupProperty property = Properties[index];
					StartupProperty newOne = new StartupProperty(property);
					newOne.Name = string.Format("{0}.{1}", Name, property.Name);

					result.Add(newOne);
				}

				return result.ToArray();
			}
		}

		public StartupProperty FindProperty(Guid propertyId)
		{
			for (int index = 0; index < Properties.Count; index++)
			{
				var property = Properties[index];

				if (property.Id == propertyId)
					return property;
			}

			return null;
		}
	}
}
